package com.lemner.www.hongyu.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lemner.www.hongyu.Bean.GetcodeBean;
import com.lemner.www.hongyu.Bean.ToverifycodeBean;
import com.lemner.www.hongyu.R;
import com.lemner.www.hongyu.Service.Service;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_phonenumber,et_code;
    private Button btn_code,btn_check,btn_return;
    private static String BASEURL="http://www.hongyudasha.com/propertysystem/";
    private Service service;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        InitView();
    }
    public void InitView(){
        et_phonenumber=(EditText)findViewById(R.id.et_phonenumber);
        et_code=(EditText)findViewById(R.id.et_code);
        btn_check=(Button)findViewById(R.id.btn_check);
        btn_code=(Button)findViewById(R.id.btn_code);
        btn_return=(Button)findViewById(R.id.btn_return);
        btn_return.setOnClickListener(this);
        btn_code.setOnClickListener(this);
        btn_check.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_code:
                if(TextUtils.isEmpty(et_phonenumber.getText())|| et_phonenumber.getText().length()!=11){
                    Toast.makeText(getApplicationContext(), "请输入正确的电话号,再试！",
                            Toast.LENGTH_SHORT).show();
                }
                else {
                    getCode();
                }
                break;
            case R.id.btn_check:
                if(TextUtils.isEmpty(et_phonenumber.getText())|| et_phonenumber.getText().length()!=11){
                    Toast.makeText(getApplicationContext(), "请输入正确的电话号,再试！",
                            Toast.LENGTH_SHORT).show();
                }
                else {
                    verificationCode();
                }
                break;
            case R.id.btn_return:
                Intent intent=new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            default:
                break;
        }
    }
    public void getCode(){
        String number=et_phonenumber.getText().toString();
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
       service=retrofit.create(Service.class);
       Call<GetcodeBean>call=service.getCode(number);
        call.enqueue(new Callback<GetcodeBean>(){

                         @Override
                         public void onResponse(Call<GetcodeBean> call, Response<GetcodeBean> response) {
                             Toast.makeText(getApplicationContext(), "发送成功!",
                                     Toast.LENGTH_SHORT).show();
                         }
                         @Override
                         public void onFailure(Call<GetcodeBean> call, Throwable t) {
                             Log.e("error", t.getMessage());
                         }
                     }
        );

    }
    public void verificationCode(){
        String number=et_phonenumber.getText().toString();
        String code=et_code.getText().toString();
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
       service=retrofit.create(Service.class);
        Call<ToverifycodeBean>call=service.verifyCode(number,code);
        call.enqueue(new Callback<ToverifycodeBean>(){

                         @Override
                         public void onResponse(Call<ToverifycodeBean> call, Response<ToverifycodeBean> response) {
                             if(TextUtils.isEmpty(et_phonenumber.getText())||TextUtils.isEmpty(et_code.getText())){
                                 Toast.makeText(getApplicationContext(), "验证失败，请重试！",
                                         Toast.LENGTH_SHORT).show();
                             }
                             else {
                                 Toast.makeText(getApplicationContext(), "验证通过，请返回！",
                                         Toast.LENGTH_SHORT).show();
                             }
                         }

                         @Override
                         public void onFailure(Call<ToverifycodeBean> call, Throwable t) {
                             Log.e("error", t.getMessage());
                         }
                     }
        );


    }
}
